package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

@Service(interfaceClass = RoleService.class)
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public PageResult findRole(Integer currentPage, Integer pageSize, String queryString) {

        PageHelper.startPage(currentPage,pageSize);
        List<Role> roles = roleMapper.findRole(queryString);

        PageInfo<Role> info = new PageInfo<>(roles);

        return new PageResult(info.getTotal(),info.getList());
    }

    @Override
    public void edit(Integer[] menuIds, Integer[] permissionIds, Role role) {
//        删除中间表数据
        roleMapper.deleteRoleAndMenuByRoleId(role.getId());
        roleMapper.deleteRoleAndPermissionByRoleId(role.getId());
        roleMapper.updateRole(role);
        roleMapper.addRoleAndMenuByMenus(role.getId(),menuIds);
        roleMapper.addRoleAndPermissionByRoleId(role.getId(),permissionIds);

    }

    @Override
    public void deleteRoleById(Integer id) {

        Role role = roleMapper.findRoleByRoleId(id);
        if ("系统管理员".equals(role.getName())) {
            throw new RuntimeException("无法删除管理员角色");
        } else {
            roleMapper.deleteRoleAndMenuByRoleId(id);
            roleMapper.deleteRoleAndPermissionByRoleId(id);
            roleMapper.deleteRoleByRoleId(id);
        }
    }

    @Override
    public void add(Integer[] menuIds, Integer[] permissionIds, Role role) {
        roleMapper.addRole(role);
        roleMapper.addRoleAndMenuByMenus(role.getId(),menuIds);
        roleMapper.addRoleAndPermissionByRoleId(role.getId(),permissionIds);
    }

    @Override
    public List<Role> findRoleAll() {
        return  roleMapper.findRoleAll();
    }

    @Override
    public List<Integer> findRoleByUserId(Integer id) {
        return roleMapper.findRoleByUserId(id);
    }
}

/***
 *                    .::::.
 *                  .::::::::.
 *                 ::::::::::: 
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 */